package cn.tx;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main extends JFrame {

    public Main() throws Exception {
        JFrame n1 = new JFrame("ͬ同学录管理系统");
        n1.setLayout(null);
        ResultSet rs = null;
        JButton b1 = new JButton("插入");
        JButton b2 = new JButton("删除");
        JButton b3 = new JButton("修改");
        JButton b4 = new JButton("查询");

        JButton b5 = new JButton("学号:");
        JButton b6 = new JButton("姓名:");
        JButton b7 = new JButton("刷新");

        JTextField txt1 = new JTextField();
        JTextField txt2 = new JTextField();
        // JTextField txt3 = new JTextField();

        JTextArea txt3 = new JTextArea();

        txt1.setFont(new Font("楷体", Font.PLAIN, 20));
        txt2.setFont(new Font("楷体", Font.PLAIN, 20));
        txt3.setFont(new Font("楷体", Font.PLAIN, 20));

        b1.setFont(new Font("楷体", Font.PLAIN, 20));
        b2.setFont(new Font("楷体", Font.PLAIN, 20));
        b3.setFont(new Font("楷体", Font.PLAIN, 20));
        b4.setFont(new Font("楷体", Font.PLAIN, 20));
        b5.setFont(new Font("楷体", Font.PLAIN, 17));
        b6.setFont(new Font("楷体", Font.PLAIN, 17));
        b7.setFont(new Font("楷体", Font.PLAIN, 17));

        n1.setBounds(20, 20, 1200, 700);

        txt1.setBounds(340, 100, 120, 40);
        txt2.setBounds(560, 100, 130, 40);
        txt3.setBounds(250, 160, 450, 280);

        b1.setBounds(150, 100, 80, 40);
        b2.setBounds(150, 200, 80, 40);
        b3.setBounds(150, 300, 80, 40);
        b4.setBounds(150, 400, 80, 40);

        b5.setBounds(259, 100, 80, 40);
        b6.setBounds(479, 100, 80, 40);
        b7.setBounds(400, 500, 80, 40);

        n1.add(b1);
        n1.add(b2);
        n1.add(b3);
        n1.add(b4);
        n1.add(b5);
        n1.add(b6);
        n1.add(b7);
        n1.add(txt1);
        n1.add(txt2);
        n1.add(txt3);

        n1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        n1.setVisible(true);

        // �������
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn = null;
                    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?characterEncoding=utf-8",
                            "root", "mysql");

                    java.sql.Statement stmt = null;
                    stmt = conn.createStatement();
                    int rs = stmt.executeUpdate("insert stu values(" + txt1.getText() + ",'" + txt2.getText() + "');");

                } catch (Exception eb) {
                    eb.getMessage();
                } finally {
                    // txt1.append("����ɹ�");
                    JOptionPane.showMessageDialog(null, "插入成功");
                }

            }
        });
        // 删除
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn = null;
                    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");

                    java.sql.Statement stmt = null;
                    stmt = conn.createStatement();
                    int rs = stmt.executeUpdate("delete from stu where id=" + txt1.getText() + ";");

                } catch (Exception et) {
                    et.getMessage();
                } finally {
                    // txt1.append("ɾ���ɹ�");
                    JOptionPane.showMessageDialog(null, "删除成功");
                }
            }
        });
        // 更新
        b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn = null;
                    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?characterEncoding=utf-8",
                            "root", "mysql");

                    java.sql.Statement stmt = null;
                    stmt = conn.createStatement();
                    int rs = stmt.executeUpdate(
                            "update stu set name='" + txt2.getText() + "' where id='" + txt1.getText() + "';");
                } catch (Exception er) {

                } finally {
                    // txt1.append("�޸ĳɹ�");
                    JOptionPane.showMessageDialog(null, "更新成功");
                }
            }
        });
        // ��ѯ����
        b4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn = null;
                    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "123456");

                    java.sql.Statement stmt = null;
                    stmt = conn.createStatement();

                    ResultSet rs = stmt.executeQuery("select * from stu");
                    while (rs.next()) {
                        txt3.append(rs.getString("id") + "  ");
                        txt3.append(rs.getString("name") + "\n");

                    }
                } catch (Exception ex) {
                    ex.getMessage();

                } finally {
                    // txt1.append("��ѯ�ɹ�")
                    JOptionPane.showMessageDialog(null, "查询成功");
                    try {
                        if (rs != null) {
                            rs.close(); // �ر�ResultSet����

                        }
                    } catch (SQLException ey) {
                        ey.printStackTrace();
                    }

                }
            }
        });
        // 查询全部
        b7.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                txt3.setText("");                txt3.setText("");
                txt1.setText("");
                txt2.setText("");
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn = null;
                    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");

                    java.sql.Statement stmt = null;
                    stmt = conn.createStatement();

                    ResultSet rs = stmt.executeQuery("select * from stu");
                    while (rs.next()) {
                        txt3.append(rs.getString("id") + "  ");
                        txt3.append(rs.getString("name") + "\n");

                    }
                } catch (Exception ex) {
                    ex.getMessage();

                } finally {
                    // txt1.append("��ѯ�ɹ�")
                    // JOptionPane.showMessageDialog(null, "��ѯ�ɹ�");
                }
            }
        });
    }
}
